package com.cos.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: COS
 * @time: 2021/11/22 17:01
 * @description:
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

    @RequestMapping("/login")
    public String login(String username,String password,@RequestParam(value = "remember-me",defaultValue = "false") Boolean rememberMe) {
        //1. 获取当前的Subject即访问用户抽象对象,调用SecurityUtils.getSubject()
        Subject subject = SecurityUtils.getSubject();
        //2. 测试当前的用户是否已经被验证，即是否登录过，Subject对象调用isAuthenticated()方法
        if (subject.isRemembered()) {
            return "index";
        }
        if (!subject.isAuthenticated()) {
            //3. 获取前端提交的用户名和密码
            //4. 若没有登录就调用UsernamePasswordToken(用户名，密码)，封装为UsernamePasswordToken对象其为AuthenticationToken的实现类
            UsernamePasswordToken token=new UsernamePasswordToken(username, password);
            System.out.println(rememberMe);
            token.setRememberMe(rememberMe);
            //5. 执行登录调用login(AuthenticationToken)
            System.out.println(token);
            subject.login(token);
        }
        return "index";
    }

}
